package qj.srt;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import qj.util.DateUtil;
import qj.util.FileUtil;
import qj.util.IOUtil;
import qj.util.funct.P1;

/**
 * Separate from 1 file to 2 files
 */
public class SubTitleSeparate {
	public static void main(String[] args) throws IOException {
//		separate();
		
		for (File file : new File("/Users/quanle/Downloads/274249").listFiles()) {
			if (file.isDirectory()) {
				continue;
			}
			if (new File("/Users/quanle/Downloads/274249/vi/" + file.getName()).exists()) {
				continue;
			}
			System.out.println("Translating: " + file.getName());
			final FileOutputStream out1 = FileUtil.fileOutputStreamUTF8("/Users/quanle/Downloads/274249/vi/" + file.getName());
			final P1<Srt> writer = Srt.writer(out1);
			Srt.readSrt(FileUtil.fileInputStream(file), new P1<Srt>() {public void e(Srt srt) {
//				String translate1 = fixTranslate(Translate.translate1(srt.text, "en", "vi"));
//				System.out.println(translate1);
//				srt.text = translate1;
//				writer.e(srt);
			}});
			IOUtil.close(out1);
		}
//		
//		String filePath = "/Users/quanle/Downloads/274249/Avatar The Last Airbender - 2x01 - The Avatar State.en.srt";
//		final FileOutputStream out1 = FileUtil.fileOutputStreamUTF8("/Users/quanle/Downloads/274249/vi/Avatar The Last Airbender - 2x01 - The Avatar State.en.srt");
//		final int[] i = {0};
//		Srt.readSrt(FileUtil.fileInputStream(new File(filePath)), new P1<Srt>() {public void e(Srt srt) {
////			System.out.println(Translate.translate1(srt.text, "en", "vi"));
////			System.exit(0);
//			try {
//				String translate1 = Translate.translate1(srt.text, "en", "vi");
//				System.out.println(translate1);
//				srt.text = translate1;
//				out1.write(srt.toString(++i[0]).getBytes("UTF-8"));
//			} catch (IOException e1) {
//				throw new RuntimeException(e1);
//			}
//		}});
//		IOUtil.close(out1);
	}
	protected static String fixTranslate(String translate1) {
		return translate1.replaceAll("<\\s*/\\s*i\\s*>", "</i>");
	}
	private static void separate() {
		String filePath = "e:/Downloads/Piranha.2010.SWESUB.AC3.DVDRip.XviD-BeNGT/Piranha 2010.srt";
		final long separate = DateUtil.parse("43:30", "mm:ss").getTime();
		
		final FileOutputStream out1 = FileUtil.fileOutputStreamUTF8(filePath.replaceAll("\\.srt$", "1.srt"));
		final FileOutputStream out2 = FileUtil.fileOutputStreamUTF8(filePath.replaceAll("\\.srt$", "2.srt"));
		FileInputStream in = FileUtil.fileInputStream(new File(filePath));
		final int[] i1={0};
		final int[] i2={0};
		Srt.readSrt(in, new P1<Srt>() {public void e(Srt srt) {
			try {
				if (srt.from < separate) {
					out1.write(srt.toString(++i1[0]).getBytes("UTF-8"));
				} else {
					out2.write(srt.back(separate).toString(++i2[0]).getBytes("UTF-8"));
				}
			} catch (IOException e1) {
				throw new RuntimeException(e1);
			}
		}});
		IOUtil.closeAll(out1, out2);
	}
	
	
}
